layui.use(['table', 'form', 'laydate', 'laytpl', 'element'], function() {
	var table = layui.table;
	form = layui.form,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		element = layui.element;
	
	/** 动态加载阶段下拉框 **/
	$.ajax({
		type : 'get',
		url : '../json/schoolType.json',
		datatype : 'json',
		success : function(schooltypedate) {
			if (schooltypedate.code == 0) {
				var str = "";
				for(var i = 0; i < schooltypedate.count; i++){
					str += '<option value=' + schooltypedate.data[i].name + '>'+ schooltypedate.data[i].name+'</option>';
				}
				$("#stage").append(str);
				form.render("select");
			}
			else{
				layer.msg('未获取到阶段信息', {icon: 7});
			}
		}
	});
	
	form.on('select(stage)', function(data){
		//layer.msg(data.value, {icon: 1});
		var schooltype = data.value;
		if(schooltype == 0){
			schooltype = '';
		}
		/** 动态加载学校下拉框 **/
		$.ajax({
			type : 'get',
			url : '../sysschoolmanagement/getschoolbywhere?schooltype=' + schooltype,
			datatype : 'json',
			success : function(schoolData) {
				if (schoolData.code == 0) {
					var str = "";
					for(var i = 0; i < schoolData.count; i++){
						str += '<option value=' + schoolData.data[i].schoolid + '>'+ schoolData.data[i].schoolName+'</option>';
					}
					$("#school").html("");
					$("#school").append(str);
					form.render("select");
				}
				else{
					$("#school").html("");
					form.render("select");
					layer.msg('学校信息获取失败', {icon: 2});
				}
			}
		});
	});
	
	form.on('select(school)', function(data){
		var schoolid = data.value;
		if(schoolid == 0){
			schoolid = '';
		}
		/** 动态加载角色下拉框 **/
		$.ajax({
			type : 'get',
			url : '../sysunifiedmanagement/rolemanagementgetrolelist?addRoleid=18' + '&schoolid=' + schoolid,
			datatype : 'json',
			success : function(roledata) {
				if (roledata.code == 0) {
					var str = "";
					for(var i = 0; i < roledata.count; i++){					
						str += '<option value=' + roledata.data[i].id + '>'+ roledata.data[i].name +'</option>';					
					}
					$("#userType").html('');
					$("#userType").append(str);
					form.render("select");
				}
				else{
					var str = "<option value='0'>请选择角色</option>";
					$("#userType").html('');
					$("#userType").append(str);
					form.render("select");
					layer.msg('暂无角色信息', {icon: 7});
				}
			}
		});
	})
	
	table.render({
		elem: '#teaclass',
		url: '../systeachermanagement/getteacherrole',
		height:"full-120",
		toolbar: '#toolbarDemo',
		title: '教师任课班级信息表',
		skin: 'line',
		even: true,
		cols: [
			[{
				field: 'realName',
				align: 'center',
				title: '教师账号'
			}, {
				field: 'gradename',
				align: 'center',
				title: '所带年级'						
			}, {
				field: 'className',
				align: 'center',
				title: '所带班级',
			}, {
				field: 'post',
				align: 'center',
				title: '职务'							
			},{
				field: 'subName',
				align: 'center',
				title: '科目'							
			},{
				field: 'phone',
				align: 'center',
				title: '联系电话'							
			},{
				title: '操作',
				toolbar: '#barDemo',
				align: 'center'
			}]
		],
		page: {
			layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
			groups: 5,
			limit: 10,
			limits: [10, 20, 30, 40, 50],
			theme: '#1E9FFF',					
		}
	});
	
	//上部工具栏事件 
	table.on('toolbar(teaclass)', function(obj) {
		var checkStatus = table.checkStatus(obj.config.id);
		var userName = $('#userName').val().trim();
		var roleid = $("#userType").val();
		switch(obj.event) {
			case 'btnseltea':
				table.render({
					elem: '#teaclass',
					url: '../systeachermanagement/getteacherrole?userName=' + userName + "&roleid=" + roleid,
					height:"full-120",
					toolbar: '#toolbarDemo',
					title: '教师任课班级信息表',
					skin: 'line',
					even: true,
					cols: [
						[{
							field: 'userName',
							align: 'center',
							title: '教师账号'
						},{
							field: 'realName',
							align: 'center',
							title: '教师姓名'							
						},{
							field: 'sex',
							align: 'center',
							title: '性别'							
						}, {
							field: 'gradename',
							align: 'center',
							title: '所带年级'						
						}, {
							field: 'className',
							align: 'center',
							title: '所带班级',
						}, {
							field: 'post',
							align: 'center',
							title: '职务'							
						},{
							field: 'subName',
							align: 'center',
							title: '科目'							
						},{
							title: '操作',
							toolbar: '#barDemo',
							align: 'center'
						}]
					],
					page: {
						layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
						groups: 5,
						limit: 10,
						limits: [10, 20, 30, 40, 50],
						theme: '#1E9FFF',					
					}
				});
				$('#userName').val(userName);
				loadDownBox(roleid);
			break;
		};
	});
	
	/*点击查看详情*/
	table.on('tool(teaclass)', function(obj) {
		var data = obj.data;
		if(obj.event === 'btnselteaclass'){
			$("#layuserName").text(data.userName);
			$("#layrealName").text(data.realName);
			$("#laysex").text(data.sex);
			$("#layphone").text(data.phone);
			$("#layroleName").text(data.roleName);
			$("#laypost").text(data.post);
			$("#laygradename").text(data.gradename);
			$("#layclassName").text(data.className);
			$("#laysubDescribe").text(data.subDescribe);
			
			layer.open({
		        type: 1, 
		        title: '教师任课班级查看',
		        area: ['600px', '420px'],
		        shade: 0.8,
		        content: $('#teachclasslay'),
		        btn: ['返回'], 
		        yes: function(){
		          layer.closeAll();
		          $(".stuclassremovelayer-con").css("display","none");
		        },
		        cancel: function(){ 
				   $(".stuclassremovelayer-con").css("display","none");
				}
		     });
		
		};
	});
});